package com.bw.zg3.Log

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3.R
import com.bw.zg3.databinding.ActivityZcactivityBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

@Route(path ="/app/ZCActivity")
@AndroidEntryPoint
class ZCActivity : BaseActivity<ActivityZcactivityBinding,UserViewModel>(){
    override fun initView() {
        binding.btn.setOnClickListener {
            val phone = binding.phone.text.toString()
           if (!RegexUtils.isMobileSimple(phone)){
               ToastUtils.showLong("手机号格式不正确")

           }else{
               //随机数
               val nextInt = Random.nextInt(1000,9999)
               ToastUtils.showLong("验证码为:$nextInt")
               SPUtils.getInstance().put("yz",nextInt)
               Log.d("验证码", "intiView: $nextInt")
               binding.btn.isEnabled=false
               lifecycleScope.launch {
                  //倒计时
                   for (i in 10 downTo 0){
                       delay(1000)
                       binding.btn.text="$i 秒后再次获取"
                       binding.btn.setTextColor(Color.WHITE)
                   }
                   binding.btn.text="再次获取验证码"
                   binding.btn.isEnabled=true
               }
           }

        }

        binding.button2.setOnClickListener {
            val phone = binding.phone.text.toString()
            val yz1 = binding.yz.text.toString()
            val pass = binding.pass.text.toString()
            val pass1 = binding.pass1.text.toString()
            val yz = SPUtils.getInstance().getInt("yz")

            if (!RegexUtils.isMobileSimple(phone)){
                ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }
            if (yz1.equals(yz)){
                ToastUtils.showLong("验证码不正确或者格式不对")
                return@setOnClickListener
            }
            if (!pass.equals(pass1)){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            vm.getZC(phone,pass1)


        }


    }

    override fun intiData() {
        vm.ZCSuccess.observe(this){
            ToastUtils.showLong("注册成功")
            ARouter.getInstance().build("/app/LogActivity").navigation()
        }
    }

}